package com.epin.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.epin.base.vo.JSTreeEntity;
import com.epin.base.vo.Select2Entity;
import com.epin.business.system.entity.bo.ResourceNode;


public class TreeUtil {
	
	/**
	 * select2下拉组件数据对象
	 */
	private List<Select2Entity> selectTree = new ArrayList<Select2Entity>();
	
	/**
	 * 生成select2下拉组件数据时遍历的次数
	 */
	private int selectCnt = 0;

	
	/**
	 * <p></p>
	 * 
	 * @param resourceList
	 * 
	 * @return List<>
	 * 
	 * @author fujiajun
	 * @since 2017-11-01
	 */
	public List<JSTreeEntity> generateJSTree(List<ResourceNode> resourceList) {
		
		List<JSTreeEntity> jstreeList = new ArrayList<JSTreeEntity>();
		
		for (ResourceNode resource : resourceList) {
			JSTreeEntity jstree = new JSTreeEntity();
			jstree.setId(resource.getId().toString());
			jstree.setParent(resource.getParentId() == null ? "#" : resource.getParentId().toString());
			jstree.setText(resource.getName());
			jstree.setIcon(StringUtils.isBlank(resource.getIcon()) ? "am-icon-cog" : resource.getIcon());
			JSTreeEntity.State state = new JSTreeEntity().new State();
			state.setDisabled(false);
			state.setSelected(resource.isSelected());
			state.setOpened(true);
			jstree.setState(state);
			jstreeList.add(jstree);
		}
		
		return jstreeList;
	}
	
	
	/**
	 * <p>根据父节点的ID获取所有子节点</p>
	 * 
	 * @param list 具有树形结构特点的集合
	 * @param parent 父节点ID
	 * 
	 * @return List<>
	 * 
	 * @author fujiajun
	 * @since 2017-11-01
	 */
	public List<Select2Entity> getSelectTree(List<ResourceNode> list, Integer parentId) {
		List<ResourceNode> treeMenuList = treeMenuList(list, parentId);
		recursionForSelect(treeMenuList);
		return selectTree;
	}
	
	
	/**
	 * <p>菜单树递归，获取菜单树资源</p>
	 * 
	 * @param menuList
	 * @param parentId
	 * 
	 * @return List<>
	 * 
	 * @author fujiajun
	 * @since 2017-11-01
	 */
	public List<ResourceNode> treeMenuList(List<ResourceNode> menuList, Integer parentId) {
		List<ResourceNode> childMenu = new ArrayList<>();
		
		for(ResourceNode menu : menuList) {
			Integer menuId = menu.getId();
			Integer pid = menu.getParentId();
			if(parentId != null && parentId.equals(pid)) {
				List<ResourceNode> c_node = treeMenuList(menuList, menuId);  
				menu.setChildren(c_node);
				childMenu.add(menu);  
			}    
		}
		
		return childMenu;  
	} 
	
	
	/**
	 * <p>递归列表</p>
	 * 
	 * @param list
	 * 
	 * @param 
	 * 
	 * @author fujiajun
	 * @since 2017-11-01
	 */
	private void recursionForSelect(List<ResourceNode> list) {
		String str = "";
		
		for(int i=0; i< selectCnt; i++) {
			str += "├┈┈┈";
		}
		
		for(ResourceNode re : list) {
			if(null == re.getParentId()) {
				str = "";
				selectCnt = 0;
			}
			
			Select2Entity se = new Select2Entity();
			se.setId(re.getId().toString());
			se.setText(str + re.getName());
			se.setName(re.getName());
			selectTree.add(se);
			
			if(re.getChildren().size() > 0) {
				selectCnt ++;
				recursionForSelect(re.getChildren());
			}
		}
	}
	
	 
	
}
